package IBBB.network.sendmessage;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.concurrent.SynchronousQueue;

public class send_register extends send_String {
    public send_register(String server, String username, String password) {
        super(server);
        target_message= "UserRegister#"+username+"#"+password;
        System.out.println(target_message);//TODO
    }

    @Override
    public Void call() {
        Socket client = new Socket();
        try {
            client.connect(message_socket, 3000);
        } catch (Exception e) {
            updateMessage("timeout");
            return null;
        }

        try (ObjectOutputStream write = new ObjectOutputStream(client.getOutputStream())) {
            try (ObjectInputStream reader = new ObjectInputStream(client.getInputStream())) {
                write.writeObject(target_message);
                String res = (String)reader.readObject();
                client.close();
                if (res.equals("FALSE")) {
                    updateMessage("nameWrong");
                } else {
                    updateMessage("success");
                }
            }
            client.close();
        } catch (Exception e) {
            updateMessage("wrong");
        }
        return null;
    }
}
